package com.demonzym.activity;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ListView;

import com.demonzym.framework.activitymanager.ActivityWindowHolder;
import com.demonzym.framework.activitymanager.BaseActivity;
import com.demonzym.framework.activitymanager.LeftFragment;
import com.demonzym.framework.activitymanager.NormalActivityManager;
import com.demonzym.framework.activitymanager.LeftFragment.OnLeftTitleClick;
import com.demonzym.framework.displaymanager.DisplayManager;
import com.demonzym.videoplayer.R;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

/**
 * sliding framework activity
 * @author Administrator
 *
 */
public class MainActivity extends SlidingFragmentActivity{
	
	private final int COMMEND = 0;

	protected LeftFragment mFrag;
	protected SlidingMenu mSlidingMenu;
	
	public static final String TAG = "MainActivity";
	
	private List<String> mTagList = new LinkedList<String>();
	private static int sTagCounter = 0;
	private String generaTag(){
		String tag = "child:"+String.valueOf(++sTagCounter);
		return tag;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		NormalActivityManager.getInstance().addActivity(this, TAG);		
		mSlidingMenu = getSlidingMenu();

		setBehindView();
		
		setSlidingMenu();
		
		setContentView(R.layout.main);
		
		startChildActivity("home", new Intent(MainActivity.this,
				VideoListActivity.class));
	}
	
	private void setBehindView() {
		setBehindContentView(R.layout.menu_frame);
//		FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
//		mFrag = new LeftFragment();
//		t.replace(R.id.menu_frame, mFrag);
//		t.commit();
//
//		mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);
//		mSlidingMenu.setShadowDrawable(R.drawable.shadow);
//		int dwidth = DisplayManager.getAbsoluteDisplayMetricsWidth(this);
//		mSlidingMenu.setBehindOffset(dwidth / 3);
//
//		if (Build.VERSION.SDK_INT >= 11) {
//			getActionBar().setDisplayHomeAsUpEnabled(true);
//		}
//		
//		mFrag.setOnLeftTitleClick(new OnLeftTitleClick() {
//			@Override
//			public void onClick(ListView l, View v, int position, long id) {
//				switch(position){
//				case COMMEND:
//					startChildActivity("child" + position, new Intent(MainActivity.this,
//							VideoListActivity.class));
//					break;
//				}
//				toggle();
//			}
//		});
	}

	private void setSlidingMenu() {
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		mSlidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_MARGIN);
		mSlidingMenu.setBehindScrollScale(0.5f);
		mSlidingMenu.setFadeDegree(1);

		setSlidingActionBarEnabled(true);
		
		mSlidingMenu.setSlidingEnabled(false);
	}

	public void startChildActivity(Intent intent) {
		startChildActivity(null, intent);
    }
    
    public void startChildActivity(String tag, Intent intent){
    	if(tag == null)
    		tag = generaTag();
    	
    	Window w = ActivityWindowHolder.mActivityManager.startActivity(tag, intent);
		View childContent = w != null ? w.getDecorView() : null;
		
		if(childContent != null){
			mTagList.add(tag);
			setContentView(childContent);
		}
		
		Activity act = ActivityWindowHolder.mActivityManager.getCurrentActivity();
		if(act != null && act instanceof BaseActivity){
    		((BaseActivity)act).setTag(tag);
		}
    }
}
